// owner-app/pages/my-door/index.js
import { handlerGobackClick, hint } from "../../../utils/util";
const app = getApp();
import Apipublic from "../../../models/api";
const apipublic = new Apipublic();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    timea: new Date().getTime(),
    photo: "",
    pictureFlag: false,
    cameraShow: false,
    devicePosition: "front",
    photoShow: false, //拍摄是否成功  拍摄：成功
    didNot: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getStorageSync("user"), "用户信息");
  },
  // watch: function (ctx, obj) {
  //   Object.keys(obj).forEach((key) => {
  //     this.observer(ctx.data, key, ctx.data[key], function (value) {
  //       obj[key].call(ctx, value);
  //     });
  //   });
  // },
  // watch: {
  //   timea: function (newVal, oldVal) {
  //     console.log(newVal, oldVal, "时间时间");
  //   },
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
      timea: new Date().getTime(),
    });

    this.initPageData();
  },
  handlerGobackClick,
  cancelPicture() {
    this.initPageData();
    this.setData({
      cameraShow: false,
    });
  },
  //重新拍摄
  toShoot() {
    this.setData({
      photo: "",
      cameraShow: true,
    });
  },
  //拍摄
  shootHandle() {
    let that = this;
    that.setData({
      photo: "",
      pictureFlag: true,
      photoShow: false,
      didNot: true,
    });

    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.camera"]) {
          that.setData({
            cameraShow: true,
          });
          that.contextPhoto();
        } else {
          // 授权
          wx.authorize({
            scope: "scope.camera",
            success() {
              that.setData({
                cameraShow: true,
              });
              that.contextPhoto();
            },
            fail(fa) {
              that.shootOpen();
            },
          });
        }
      },
    });
  },
  shootOpen() {
    let that = this;
    wx.openSetting({
      success(res) {
        if (res.authSetting["scope.camera"]) {
          that.setData({
            cameraShow: true,
          });
          that.contextPhoto();
        }
      },
    });
  },
  contextPhoto() {
    let that = this;
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality: "high",
      success: (res) => {
        that.setData({
          cameraShow: false,
          photo: res.tempImagePath,
        });

        that.miniImage();
      },
    });
  },
  // 将微信返回的临时路径转为base64
  miniImage() {
    let _this = this;
    wx.getFileSystemManager().readFile({
      filePath: _this.data.photo,
      encoding: "base64",
      success(res) {
        let data = {
          faceBase64: "data:image/jpeg;base64," + res.data,
          commId: wx.getStorageSync("commid"),
          idCard: wx.getStorageSync("user").idcard,
          name: wx.getStorageSync("user").realname,
        };

        let signBool = true;

        let header = {
          // "Content-Type": "multipart/form-data",   //不要删，后台暂时不需要
          "X-Access-Token": wx.getStorageSync("token"),
        };

        apipublic.userCheckPic(data, header).then((res) => {
          if (res.code == 1000) {
            signBool = false;
            _this.setData({
              photoShow: true,
              didNot: false,
              facePath: res.result,
            });
          } else if (res.code == 500) {
            console.log(res, "错误的AAA");
          }
        });

        if (signBool) {
          _this.setData({
            photoShow: false,
            didNot: true,
          });
        }
      },
    });
  },
  // 摄像头转换
  cameraConversion() {
    if (this.data.devicePosition == "front") {
      this.setData({
        devicePosition: "back",
      });
    } else {
      this.setData({
        devicePosition: "front",
      });
    }
  },
  error(e) {
    console.log(e.detail, "错了");
  },
  //保存
  submitLastAll() {
    wx.switchTab({
      url: "/pages/my/index",
    });
  },
  initPageData() {
    let user = wx.getStorageSync("user");
    let data = {
      idCard: user.idcard,
      memberId: user.memberId,
    };
    apipublic.userInfoDetail(data).then((res) => {
      if (res.code == 1000) {
        this.setData({
          photo: res.result.facePath + "?time=" + this.data.timea,
          pictureFlag: res.result.facePath ? false : true,
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      cameraShow: false,
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
